package com.ybxx.core.excel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

/**
 * Excel 报表工具类
 * */
public class ExportExcel {

	/**
	 * 创建通用EXCEL头部
	 * 
	 * @param headString 头部显示的字符
	 * @param colSum 该报表的列数
	 */
	public void createNormalHead(HSSFWorkbook wb,HSSFSheet sheet,String headString, int colSum) {
		HSSFRow row = (HSSFRow) sheet.createRow(0);
		// 设置第一行
		HSSFCell cell = (HSSFCell) row.createCell(0);
		row.setHeight((short) 500);
		// 定义单元格为字符串类型
		cell.setCellType(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue(new HSSFRichTextString(headString));
		// 指定合并区域
		sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum));
		HSSFCellStyle cellStyle = (HSSFCellStyle) wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setWrapText(true);// 指定单元格自动换行
		// 设置单元格字体
		HSSFFont font = (HSSFFont) wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 300);
		cellStyle.setFont(font);
		cell.setCellStyle(cellStyle);
	}

	/**
	 * 设置报表标题
	 * @param wb   工作薄
	 * @param sheet 工作表
	 * @param columHeader 标题字符串数组
	 * @param rowHeight 行高
	 * @param 单元格自动换行标识 true:是 false :否
	 */
	public void createColumHeader(HSSFWorkbook wb,HSSFSheet sheet,String[] columHeader,int rowHeight,boolean sign) {
		// 设置列头
		HSSFRow row1 = (HSSFRow) sheet.createRow(1);
		// 指定行高
		row1.setHeight((short) rowHeight);
		HSSFCellStyle cellStyle = (HSSFCellStyle) wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//下边框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//左边框
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//右边框
		cellStyle.setWrapText(sign);// 指定单元格自动换行
		// 单元格字体
		HSSFFont font = (HSSFFont) wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 250);
		cellStyle.setFont(font);
		// 设置单元格背景色
		cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		HSSFCell cell3 = null;
		for (int i = 0; i < columHeader.length; i++) {
			cell3 = (HSSFCell) row1.createCell(i);
			cell3.setCellType(HSSFCell.ENCODING_UTF_16);
			cell3.setCellStyle(cellStyle);
			cell3.setCellValue(new HSSFRichTextString(columHeader[i]));
		}

	}
	
	/**
	 * 创建内容单元格
	 * 
	 * @param wb HSSFWorkbook
	 * @param row HSSFRow
	 * @param col short型的列索引
	 * @param align 对齐方式
	 * @param val 列值
	 */
	public void cteateCell(HSSFCellStyle cellstyle, HSSFRow row, int col,
			short align, String val) {
		HSSFCell cell = (HSSFCell) row.createCell(col);
		cell.setCellType(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue(new HSSFRichTextString(val));
		//HSSFCellStyle cellstyle = (HSSFCellStyle) wb.createCellStyle();
		cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//下边框
		cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//左边框
		cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//右边框
		cellstyle.setAlignment(align);
		cell.setCellStyle(cellstyle);
	}

	/**
	 * 以输出流的方式直接导出(服务器端不生成文件)
	 * @param output 输出流
	 * */
	public void outputExcel(HSSFWorkbook wb,OutputStream output) throws IOException{
        output.flush();
        wb.write(output);
        output.close();
	}

	/**
	 * 以文件输出流方式创建EXCEL文件(服务端生成文件)
	 * @param filePath 文件路径
	 * @param fileName 文件名
	 */
	public void createExcel(HSSFWorkbook wb,String filePath,String fileName) {
		FileOutputStream fos = null;
		try {
			File pathFile = new File(filePath);
			if(!pathFile.exists()){
				pathFile.mkdirs();
			}
			fos = new FileOutputStream(new File(filePath+"/"+fileName));
			wb.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
